# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: test3.py
Author: kij
Create Date: 2021/3/7
-------------------------------------------------
"""
all_menu_list = [
	{"id": 1, "title": "校区管理"},
	{"id": 2, "title": "客户管理"},
	{"id": 3, "title": "学员管理"},
	{"id": 4, "title": "权限管理"},
]

permission_list = [
	{"id": 1, "title": "校区列表", "url": "http://127.0.0.1:8000/school/list/", "parent_id": None, "menu_id": 1},
	{"id": 2, "title": "修改", "url": "http://127.0.0.1:8000/school/edit/", "parent_id": 1, "menu_id": None},
	{"id": 9, "title": "课程列表", "url": "http://127.0.0.1:8000/course/edit/", "parent_id": None, "menu_id": 1},
	{"id": 10, "title": "添加课程", "url": "http://127.0.0.1:8000/course/add/", "parent_id": 9, "menu_id": None},
]

# for e in all_menu_list:
# 	e['child'] = [pe['title'] for pe in permission_list if pe['menu_id'] == e['id']]
# print(all_menu_list)
# 第一题
menu_list = [
	# {"id": 1, "title": "校区管理", "child": ["校区列表", "课程列表"]},
	# {"id":2,"title":"客户管理"...},
	# {"id":3,"title":"学员管理"..},
	# {"id":4,"title":"权限管理"...},
]
# # 循环列表拿到字典
# result_list = {}
# for item in permission_list:
# 	# 左边的列表
# 	for menu in all_menu_list:
# 		if item['menu_id'] == menu['id']:
# 			if 'child' not in menu:
# 				menu.setdefault('child', [])
# 			menu['child'].append(item['title'])
# 			print(menu)

# 第二题
# permission_list = [
# 	{"id": 1, "title": "校区列表", "url": "http://127.0.0.1:8000/school/list/", "parent_id": None, "menu_id": 1, 'child': [
# 		{"id": 2, "title": "修改", "url": "http://127.0.0.1:8000/school/edit/", "parent_id": 1, "menu_id": None},
# 	]},
# 	{"id": 9, "title": "课程列表", "url": "http://127.0.0.1:8000/course/edit/", "parent_id": None, "menu_id": 1, 'child': [
# 		{"id": 10, "title": "添加课程", "url": "http://127.0.0.1:8000/course/add/", "parent_id": 9, "menu_id": None},
# 	]},
# ]

root_list = []
permission_dict = {}
for item in permission_list:
	item['child'] = []
	key = item['id']
	permission_dict[key] = item
	if not item['parent_id']:
		root_list.append(item)

for ele in permission_list:
	parent_id = ele['parent_id']
	if not parent_id:
		continue
		permission_dict[parent_id]['child'].append(ele)
print(permission_dict)
# 来吧
# {666: [11, 22, 33]}
# [11, 22, 33]
# {666: [11, 22, 33, 999]}
# [11, 22, 33, 999]
